package com.klm.easymq.health;

import com.klm.easymq.core.adapter.MQAdapter;
import com.klm.easymq.core.adapter.factory.MQAdapterFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Easy-MQ 健康检查指示器
 * 集成 Spring Boot Actuator，提供MQ连接状态检查
 */
@Slf4j
@Component
public class EasyMqHealthIndicator implements HealthIndicator {
    
    @Autowired
    private MQAdapterFactory mqAdapterFactory;
    
    @Override
    public Health health() {
        try {
            Map<String, Object> details = new HashMap<>();
            boolean allHealthy = true;
            
            // 获取所有支持的适配器类型
            Set<String> supportedTypes = mqAdapterFactory.getSupportedTypes();
            details.put("supportedTypes", supportedTypes);
            
            // 检查每个适配器的健康状态
            Map<String, Object> adapterHealth = new HashMap<>();
            for (String type : supportedTypes) {
                try {
                    MQAdapter adapter = mqAdapterFactory.getAdapter(type);
                    if (adapter != null) {
                        boolean isHealthy = adapter.isHealthy();
                        adapterHealth.put(type, Map.of(
                            "healthy", isHealthy,
                            "status", adapter.getStatus().name()
                        ));
                        
                        if (!isHealthy) {
                            allHealthy = false;
                        }
                    } else {
                        adapterHealth.put(type, Map.of(
                            "healthy", false,
                            "status", "NOT_AVAILABLE"
                        ));
                        allHealthy = false;
                    }
                } catch (Exception e) {
                    log.warn("检查适配器健康状态失败: type={}", type, e);
                    adapterHealth.put(type, Map.of(
                        "healthy", false,
                        "status", "ERROR",
                        "error", e.getMessage()
                    ));
                    allHealthy = false;
                }
            }
            
            details.put("adapters", adapterHealth);
            details.put("totalAdapters", supportedTypes.size());
            
            if (allHealthy) {
                return Health.up()
                    .withDetails(details)
                    .build();
            } else {
                return Health.down()
                    .withDetails(details)
                    .build();
            }
            
        } catch (Exception e) {
            log.error("健康检查失败", e);
            return Health.down()
                .withDetail("error", e.getMessage())
                .build();
        }
    }
} 